package com.zscat.mallplus.sms.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zscat.mallplus.annotation.SysLog;
import com.zscat.mallplus.sms.entity.SmsHomeAdvertise;
import com.zscat.mallplus.sms.service.ISmsHomeAdvertiseService;
import com.zscat.mallplus.utils.CommonResult;
import com.zscat.mallplus.utils.ValidatorUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

/**
 * 首页轮播广告管理Controller
 * https://gitee.com/zscat-platform/mall on 2018/11/7.
 */
@Controller
@Api(tags = "SmsHomeAdvertiseController", description = "首页轮播广告管理")
@RequestMapping("/sms/SmsHomeAdvertise")
@Slf4j
public class SmsHomeAdvertiseController {
    @Autowired
    private ISmsHomeAdvertiseService advertiseService;


    @SysLog(MODULE = "sms", REMARK = "根据条件查询所有广告位列表")
    @ApiOperation("根据条件查询所有广告位列表")
    @GetMapping(value = "/list")
    @ResponseBody
    public Object getSmsHomeAdvertiseByPage(SmsHomeAdvertise entity,
                                        @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                        @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize
    ) {
        try {
            return new CommonResult().success(advertiseService.page(new Page<SmsHomeAdvertise>(pageNum, pageSize), new QueryWrapper<>(entity)));
        } catch (Exception e) {
            log.error("根据条件查询所有广告位列表：%s", e.getMessage(), e);
        return new CommonResult().failed();
    }
    }

    @ApiOperation("添加广告")
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    @ResponseBody
    public Object create(@RequestBody SmsHomeAdvertise advertise) {
        int count = advertiseService.create(advertise);
        if (count > 0)
            return new CommonResult().success(count);
        return new CommonResult().failed();
    }

    @SysLog(MODULE = "sms", REMARK = "查看广告位详情")
    @ApiOperation("查看广告位详情")
    @GetMapping(value = "/{id}")
    @ResponseBody
    public Object getSmsHomeAdvertiseById(@ApiParam("id") @PathVariable Long id) {
        try {
            if (ValidatorUtils.empty(id)) {
                return new CommonResult().paramFailed("id");
            }
            SmsHomeAdvertise homeAdvertise = advertiseService.getById(id);
            return new CommonResult().success(homeAdvertise);
        } catch (Exception e) {
            log.error("查询明细：%s", e.getMessage(), e);
            return new CommonResult().failed();
        }

    }

    @SysLog(MODULE = "sms", REMARK = "更新广告位")
    @ApiOperation("更新广告位")
    @PostMapping(value = "/update/{id}")
    @ResponseBody
    public Object updateSmsHomeAdvertise(@RequestBody SmsHomeAdvertise advertise) {
        try {
            if (advertiseService.updateById(advertise)) {
                return new CommonResult().success();
            }
        } catch (Exception e) {
            log.error("更新：%s", e.getMessage(), e);
            return new CommonResult().failed();
        }
        return new CommonResult().failed();
    }




}
